#define _CRT_SECURE_NO_WARNINGS 1

//用栈实现队列
typedef struct
{
    Stack s1;
    Stack s2;
} MyQueue;

//队列的初始化
MyQueue* myQueueCreate()
{
    MyQueue* mq = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&(mq->s1));
    StackInit(&(mq->s2));

    return mq;
}

//入队尾
void myQueuePush(MyQueue* obj, int x)
{
    //往非空的栈中入数据
    if (!StackEmpty(&(obj->s1)))
    {
        //s1非空
        StackPush(&(obj->s1), x);
    }
    else
    {
        //s2非空
        StackPush(&(obj->s2), x);
    }
}

//从队头出数据
int myQueuePop(MyQueue* obj)
{
    //假设法，给出空栈和非空栈
    MyQueue* empty = &(obj->s1);
    MyQueue* noempty = &(obj->s2);
    if (!StackEmpty(empty))
    {
        empty = &(obj->s2);
        noempty = &(obj->s1);
    }

    //将非空栈中的后size-1个数据拷贝到空栈中
    while (GetStackSize(noempty) > 1)
    {
        //1.将非空栈数据push到空栈中
        StackPush(empty, GetStackTop(noempty));

        //2.pop掉已经导入的数据
        StackPop(noempty);
    }
    //记录并删除原栈中栈顶的数据
    int top = GetStackTop(noempty);
    StackPop(noempty);

    //将数据重新复制到原栈中
    while (GetStackSize(empty))
    {
        StackPush(noempty, GetStackTop(empty));
        StackPop(empty);
    }

    return top;
}

int myQueuePeek(MyQueue* obj)
{
    //假设法，给出空栈和非空栈
    MyQueue* empty = &(obj->s1);
    MyQueue* noempty = &(obj->s2);
    if (!StackEmpty(empty))
    {
        empty = &(obj->s2);
        noempty = &(obj->s1);
    }

    //将数据拷贝到另一个栈中
    while (GetStackSize(noempty))
    {
        StackPush(empty, GetStackTop(noempty));
        StackPop(noempty);
    }

    //另一个栈中的栈顶元素就是队列的队头元素
    int top = GetStackTop(empty);

    //将数据重新拷贝回去
    while (GetStackSize(empty))
    {
        StackPush(noempty, GetStackTop(empty));
        StackPop(empty);
    }

    //返回队头元素
    return top;
}

bool myQueueEmpty(MyQueue* obj)
{
    return StackEmpty(&(obj->s1)) && StackEmpty(&(obj->s2));
}

void myQueueFree(MyQueue* obj)
{
    StackDestory(&(obj->s1));
    StackDestory(&(obj->s2));

    free(obj);
    obj = NULL;
}


//设计循环队列

typedef struct
{
    int* a;
    int head;//指向头
    int tail;//指向尾
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* mcq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));

    mcq->a = (int*)malloc(sizeof(int) * (k + 1));
    mcq->head = 0;
    mcq->tail = 0;
    mcq->k = k;

    return mcq;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    return obj->head == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    return (obj->tail + 1) % (obj->k + 1) == obj->head;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    //判满，队列没满才可以插入
    if (myCircularQueueIsFull(obj))
    {
        //队列满
        return false;
    }
    else
    {
        //队列没满
        //直接在tail位置上插入
        obj->a[obj->tail++] = value;
    }
    //避免tail越界
    obj->tail %= obj->k + 1;
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        //如果队列为空，则无法删除，返回false
        return false;
    }
    else
    {
        obj->head++;
    }
    obj->head %= obj->k + 1;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[obj->head];
    }
}

int myCircularQueueRear(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[(obj->tail - 1 + obj->k + 1) % (obj->k + 1)];
    }
}



void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->a);
    obj->a = NULL;
    obj->head = 0;
    obj->tail = 0;
    obj->k = 0;

    free(obj);
    obj = NULL;
}